package unimelb.discussiontracker.twitter.service.integration;

import java.io.File;
import java.io.FileFilter;

import org.apache.commons.io.filefilter.RegexFileFilter;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationObserver;

public class FolderCreateListener implements FileAlterationListener {

    private String topicFilePattern;

    private String topTweetKeywordFilePattern;

    private String topTweetUserFilePattern;

    private String topTweetTagFilePattern;

    private String topUserKeywordFilePattern;

    private String topTagKeywordFilePattern;

    private IFileProcessor topicIntervalProcessor;

    private IFileProcessor topUsersProcessor;

    private IFileProcessor topTagsProcessor;

    private IFileProcessor topTweetsKeywordBasedProcessor;

    private IFileProcessor topTweetsUsersBasedProcessor;

    private IFileProcessor topTweetsHashBasedProcessor;

    public void setTopicFilePattern(String topicFilePattern) {
        this.topicFilePattern = topicFilePattern;
    }

    public void setTopTweetKeywordFilePattern(String topTweetKeywordFilePattern) {
        this.topTweetKeywordFilePattern = topTweetKeywordFilePattern;
    }

    public void setTopTweetUserFilePattern(String topTweetUserFilePattern) {
        this.topTweetUserFilePattern = topTweetUserFilePattern;
    }

    public void setTopTweetTagFilePattern(String topTweetTagFilePattern) {
        this.topTweetTagFilePattern = topTweetTagFilePattern;
    }

    public void setTopUserKeywordFilePattern(String topUserKeywordFilePattern) {
        this.topUserKeywordFilePattern = topUserKeywordFilePattern;
    }

    public void setTopTagKeywordFilePattern(String topTagKeywordFilePattern) {
        this.topTagKeywordFilePattern = topTagKeywordFilePattern;
    }

    public void setTopicIntervalProcessor(IFileProcessor topicIntervalProcessor) {
        this.topicIntervalProcessor = topicIntervalProcessor;
    }

    public void setTopUsersProcessor(IFileProcessor topUsersProcessor) {
        this.topUsersProcessor = topUsersProcessor;
    }

    public void setTopTagsProcessor(IFileProcessor topTagsProcessor) {
        this.topTagsProcessor = topTagsProcessor;
    }

    public void setTopTweetsKeywordBasedProcessor(IFileProcessor topTweetsKeywordBasedProcessor) {
        this.topTweetsKeywordBasedProcessor = topTweetsKeywordBasedProcessor;
    }

    public void setTopTweetsUsersBasedProcessor(IFileProcessor topTweetsUsersBasedProcessor) {
        this.topTweetsUsersBasedProcessor = topTweetsUsersBasedProcessor;
    }

    public void setTopTweetsHashBasedProcessor(IFileProcessor topTweetsHashBasedProcessor) {
        this.topTweetsHashBasedProcessor = topTweetsHashBasedProcessor;
    }

    @Override
    public void onDirectoryChange(File directory) {
    }

    @Override
    public void onDirectoryCreate(File directory) {
        final String masterTopic = directory.getName();

        invokeProcessor(directory, topicFilePattern, topicIntervalProcessor, masterTopic);

        invokeProcessor(directory, topTagKeywordFilePattern, topTagsProcessor, masterTopic);

        invokeProcessor(directory, topTweetKeywordFilePattern, topTweetsKeywordBasedProcessor, masterTopic);
        invokeProcessor(directory, topTweetTagFilePattern, topTweetsHashBasedProcessor, masterTopic);
        invokeProcessor(directory, topTweetUserFilePattern, topTweetsUsersBasedProcessor, masterTopic);

        invokeProcessor(directory, topUserKeywordFilePattern, topUsersProcessor, masterTopic);

        directory.delete();
    }

    private void invokeProcessor(File directory, String filterPattern, IFileProcessor processor, String masterTopic) {
        for (final File file : directory.listFiles((FileFilter) new RegexFileFilter(filterPattern))) {
            processor.process(file, masterTopic);
            file.delete();
        }
    }

    @Override
    public void onDirectoryDelete(File directory) {
    }

    @Override
    public void onFileChange(File file) {
    }

    @Override
    public void onFileCreate(File file) {
    }

    @Override
    public void onFileDelete(File file) {
    }

    @Override
    public void onStart(FileAlterationObserver observer) {
    }

    @Override
    public void onStop(FileAlterationObserver observer) {
    }

}
